require "Battle/Skill/SkillEffect"
require "Battle/Skill/SkillSpellFly"
require "Battle/Skill/SkillSpellLightning"

ActionPlayer = {}
local this = ActionPlayer

function ActionPlayer.init()
	this.effects = {}
	this.spellFlys = {}
	this.spellLightning = {}

	this.delEffects = {}
	this.delSpellFly = {}
	this.delSpellLightning = {}

	this.autoIncId = 0

	UpdateBeat:Add(this.update)
end

function ActionPlayer.playEffect(effect, role)
	this.autoIncId = this.autoIncId + 1
	local skillEffect = SkillEffect.new(this.autoIncId, effect, role)
	this.effects[this.autoIncId] = skillEffect
	skillEffect:play()
end

function ActionPlayer.destroyEffect(effectId)
	local skillEffect = this.effects[effectId]
	if(skillEffect ~= nil) then
		skillEffect:destroy()
		this.effects[effectId] = nil
	end
end

function ActionPlayer.playSpellFly(spell, role, target)
	this.autoIncId = this.autoIncId + 1
	local skillSpell = SkillSpellFly.new(this.autoIncId, spell, role, target)
	this.spellFlys[this.autoIncId] = skillSpell
	skillSpell:play()
end

function ActionPlayer.destroySpellFly(spellId)
	local skillSpell = this.spellFlys[spellId]
	skillSpell:destroy()
	this.spellFlys[spellId] = nil
end

function ActionPlayer.playSpellLightning(spell, role, target)
	this.autoIncId = this.autoIncId + 1
	local skillSpell = SkillSpellLightning.new(this.autoIncId, spell, role, target)
	this.spellLightning[this.autoIncId] = skillSpell
	skillSpell:play()
end

function ActionPlayer.destroySpellLightning(spellId)
	local skillSpell = this.spellLightning[spellId]
	skillSpell:destroy()
	this.spellLightning(spellId)
end

function ActionPlayer.clear()
	this.effects = nil
	this.spellFlys = nil
	this.spellLightning = nil

	this.delEffects = nil
	this.delSpellFly = nil
	this.delSpellLightning = nil

	UpdateBeat:Remove(this.update)
end

function ActionPlayer.update()
	if(this.delEffects ~= nil) then
		for _,v in ipairs(this.delEffects) do
			this.destroyEffect(v)
		end
		tableUtil.clearTable(this.delEffects)
	end

	if(this.delSpellFly) then
		for _,v in ipairs(this.delSpellFly) do
			this.destroySpellFly(v)
		end
		tableUtil.clearTable(this.delSpellFly)
	end

	if(this.delSpellLightning) then
		for _,v in ipairs(this.delSpellLightning) do
			this.destroySpellLightning(v)
		end
		tableUtil.clearTable(this.delSpellLightning)
	end
end

function ActionPlayer.removeEffect(effectId)
	table.insert(this.delEffects, effectId)
end

function ActionPlayer.removeSpellFly(spellId)
	table.insert(this.delSpellFly, spellId)
end

function ActionPlayer.removeSpellLightning(spellId)
	table.insert(this.delSpellLightning, spellId)
end